package com.lznb.cloud.service.impl;

import com.lznb.cloud.entity.Users;
import com.lznb.cloud.repository.UsersRepository;
import com.lznb.cloud.service.UsersService;
import com.lznb.cloud.utils.JsonResultExt;
import com.lznb.cloud.utils.SQLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@CacheConfig(cacheNames="users")
public class UsersServiceImpl implements  UsersService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private  UsersRepository usersRepository;


    @Override
    @CachePut(key = "#root.caches[0].name + ':' + #p0")
    public  Users getUsers(String usersId) {
        return usersRepository.findOne(usersId);
    }

    @Override
    public JsonResultExt save( Users users) {
        JsonResultExt jsonResultExt=new JsonResultExt();
         jsonResultExt.run(jsonResultExt,usersRepository.save(users));
        return jsonResultExt;
    }

    @Override
    @CachePut( key = "#root.caches[0].name + ':' + #p0.usersId")
    public JsonResultExt update( Users users) {
        JsonResultExt jsonResultExt=new JsonResultExt();
        jsonResultExt.run(jsonResultExt, usersRepository.saveAndFlush(users));
        return jsonResultExt;
    }

    @Override
    @CacheEvict(key = "#root.caches[0].name + ':' + #p0")
    public JsonResultExt delete(String usersId) {
        JsonResultExt jsonResultExt=new JsonResultExt();
        usersRepository.delete(usersId);
        return jsonResultExt;
    }

    @Override
    public Map findByPage(Integer pageIndex, Integer size, Users users,Map<String,String> condition) {
        Sort sort = new Sort(Sort.Direction.ASC, "usersSort").and(new Sort(Sort.Direction.DESC, "updateTime"));
        Pageable pageable = new PageRequest(pageIndex, size, sort);
        Specification< Users> specification = this.handleSpeci(users,condition);
        Page< Users> list = usersRepository.findAll(specification,pageable);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.getTotalElements());
        map.put("data",list.getContent());

        return map;
    }

    /**
     * 登录
     * @param username
     * @param pwd
     * @return
     */
    @Override
    public Users login(String username, String pwd) {
        return usersRepository.login(username, pwd);
    }


    public void deleteInBatch(Iterable< Users> usersIterable) {
         usersRepository.deleteInBatch(usersIterable);
    }


    /***
    * 考查询条件处理<br>
    * @param users {@link  Users} 查询条件实体
    * @return {@link Specification}
    */
    public Specification< Users> handleSpeci( Users users,Map<String,String> condition) {
        Specification< Users> specification = new Specification< Users>() {
            @Override
            public Predicate toPredicate(Root< Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = SQLUtils.CriteriaSQLExt(root,users,criteriaBuilder,condition);

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return specification;
    }
}
